package com.clj.blesample;

import com.clj.blesample.entity.Record;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class Test {
    public static void main(String[] args) {

//        DecimalFormat decimalFormat = new DecimalFormat("0000");
//        String format = decimalFormat.format(1);
//        System.out.println(format);
        System.out.println(Math.round(2.45));
        try {
            List<Record> records = new ArrayList<>();
            records.add(new Record(1.0f, 2.0f, "2023-02-03 10:10:00"));
            records.add(new Record(1.2f, 2.0f, "2023-02-03 10:10:00"));
            records.add(new Record(1.2f, 2.1f, "2023-02-03 10:10:00"));
            records.add(new Record(1.3f, 2.0f, "2023-02-03 10:10:00"));
            records.add(new Record(1.0f, 2.3f, "2023-02-03 10:10:00"));
            records.add(new Record(1.1f, 2.4f, "2023-02-03 10:10:00"));
            records.add(new Record(1.0f, 2.0f, "2023-02-03 10:10:00"));
            records.add(new Record(1.0f, 2.0f, "2023-02-03 10:10:00"));
            records.add(new Record(1.0f, 2.0f, "2023-02-03 10:10:00"));
            records.add(new Record(1.0f, 2.0f, "2023-02-03 10:10:00"));
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
            String fileName = Integer.parseInt("23121123", 16) + "-" + format.format(System.currentTimeMillis()) + ".txt";
            File cacheFile = new File("D:\\\\", fileName);
            if (cacheFile.exists()) {
                cacheFile.delete();
            }
            OutputStreamWriter bfw = new OutputStreamWriter(new FileOutputStream(cacheFile, true), StandardCharsets.UTF_8);
            DecimalFormat decimalFormat = new DecimalFormat("0000");
            for (int i = 0; i < records.size(); i++) {
                Record record = records.get(i);
                float v = record.electric * record.voltage;
                bfw.write(decimalFormat.format(i + 1) + "," + Math.round(v));
                bfw.write(System.getProperty("line.separator"));
            }
            bfw.flush();
            bfw.close();
        } catch (Exception e) {

        }
    }
}
